#include "record.h"

Record::Record()
{

}

Record::~Record() //一定要防止产生空悬指针否则在尝试删除空悬指针的时候程序会崩溃
{
    if(!recordBackup.empty())
    {
        map<int, Step*>::iterator iter_o;
        map<int, Step*>::iterator iter_bu = recordBackup.begin();
        while (iter_bu!=recordBackup.end())
        {
            int index = iter_bu->first;
            iter_o = record.find(index);
            if(iter_bu->second != 0 && (iter_bu->second)!=(iter_o->second))
            {
                delete iter_bu->second;
            }
            iter_bu++;
        }
    }

    if(!record.empty())
    {
        map<int, Step*>::iterator iter = record.begin();
        while (iter!=record.end())
        {
            if(iter->second != 0)
            {
                delete iter->second;
            }
            iter++;
        }
    }
}

void Record::addStep(Step *step)
{
    unsigned int i = step->getIndex();
    record.insert(map<int, Step*>::value_type(i, step));
}

Step* Record::getStep(int i)
{
    map<int, Step*>::iterator iter;
    iter = record.find(i);
    if(iter!=record.end())
    {
        return iter->second;
    }
    else return 0;
}

Step* Record::getLastStep() //调用它的函数需要额外保证返回的是非空指针************
{
    if(!record.empty())
    {
        map<int, Step*>::iterator iter = record.end();
        iter--;
        return iter->second;
    }
    else return 0;
}

Step* Record::getLastStepBU() //调用它的函数需要额外保证返回的是非空指针************
{
    if(!recordBackup.empty())
    {
        map<int, Step*>::iterator iter = recordBackup.end();
        iter--;
        return iter->second;
    }
    else return 0;
}

void Record::addStepBU(Step *step)
{
    unsigned int i = step->getIndex();
    recordBackup.insert(map<int, Step*>::value_type(i, step));
}

Step* Record::getStepBU(int i)
{
    map<int, Step*>::iterator iter;
    iter = recordBackup.find(i);
    if(iter!=recordBackup.end())
    {
        return iter->second;
    }
    else return 0;
}

void Record::copyToBackup()
{
    if(!record.empty())
    {
        recordBackup = record;
    }
}

void Record::deleteFrom(int i)
{
    map<int, Step*>::iterator iter_bu, iter_o;
    for(iter_bu = recordBackup.find(i); iter_bu!=recordBackup.end(); iter_bu++)
    {
        int index = iter_bu->first;
        iter_o = record.find(index);
        if((iter_bu->second) != (iter_o->second))
        {
            delete iter_bu->second;
        }
    }
    iter_bu = recordBackup.find(i);
    if(iter_bu != recordBackup.end())
    {
        recordBackup.erase(iter_bu, recordBackup.end());
    }
}

void Record::clearBackup()
{
    if(!recordBackup.empty())
    {
        map<int, Step*>::iterator iter_o;
        map<int, Step*>::iterator iter_bu = recordBackup.end();
        iter_bu--;
        int max = iter_bu->first;
        iter_o = record.find(max);

        while((iter_bu->second)!=(iter_o->second) && max!=0 )
        {
            delete iter_bu->second;
            max--; //这个方法成立是因为key值不可能跳跃, 必然是一手一手连续的
            iter_o = record.find(max);
            iter_bu = recordBackup.find(max);
        }
        recordBackup.clear();
    }
}

coordinate Record::regretRecord() //不能被直接调用, 通过go类中的函数间接调用, 因为涉及到还原go类中的两个指针变量
{
    coordinate coo;
    if(!record.empty())
    {
        map<int, Step*>::iterator iter = record.end();
        iter--;
        coo.first = iter->second->getCoordinateX();
        coo.second = iter->second->getCoordinateY();

        delete iter->second;
        record.erase(iter);

        return coo;
    }
    else
    {
        coo.first = ERROR;
        coo.second = ERROR;

        return coo;
    }
}
